<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人" prop="corporation">
        <el-input
          v-model="queryParams.corporation"
          placeholder="请输入法人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信用代码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入社会统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:enterprise:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:enterprise:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:enterprise:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:enterprise:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id"  width="50"/>
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="法人" align="center" prop="corporation" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="社会统一信用代码" align="center" prop="creditCode" width="160"/>
      <el-table-column label="区划编码" align="center" prop="areaName" width="260"/>
      <el-table-column label="企业专员" align="center" prop="enterpriseCommissionerName" />
      <el-table-column label="企业规模" align="center" prop="enterpriseSize">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.enterprise_size"
            :value="scope.row.enterpriseSize"
          />
        </template>
      </el-table-column>
      <el-table-column label="行业类型" align="center" prop="industryType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.industry_type"
            :value="scope.row.industryType"
          />
        </template>
      </el-table-column>
      <el-table-column label="通过时间" align="center" prop="transitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:enterprise:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:enterprise:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人" prop="corporation">
              <el-input v-model="form.corporation" placeholder="请输入法人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="区划编码" prop="areaCode">
            <el-cascader
              placeholder="请选择区划编码"
              v-model="form.areaCode"
              :options="deptlist"
              :props="{ checkStrictly: true }"  
              filterable
              clearable></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业专员" prop="enterpriseCommissioner">
              <el-select
                v-model="form.enterpriseCommissioner"
                placeholder="请选择企业专员"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in userlist"
                  :key="dict.userId"
                  :label="dict.nickName"
                  :value="dict.userId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="企业规模" prop="enterpriseSize">
              <el-select
                v-model="form.enterpriseSize"
                placeholder="请选择企业规模"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in dict.type.enterprise_size"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业类型" prop="industryType">
              <el-select
                v-model="form.industryType"
                placeholder="请选择行业类型"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in dict.type.industry_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="信用代码" prop="creditCode">
              <el-input v-model="form.creditCode" placeholder="请输入社会统一信用代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通过时间" prop="transitTime">
              <el-date-picker clearable
                v-model="form.transitTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择通过时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEnterprise, getEnterprise, delEnterprise, addEnterprise, updateEnterprise } from "@/api/system/enterprise";
import { getDeptListByJms } from "@/api/system/news";
import { listUser } from "@/api/system/user";
export default {
  name: "Enterprise",
  dicts:[
    "enterprise_size",
    "industry_type"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      enterpriseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        corporation: null,
        contactPerson: null,
        contactPhone: null,
        creditCode: null,
        areaCode: null,
        enterpriseSize: null,
        industryType: null,
        enterpriseScale: null,
        createName: null,
        updateName: null,
        transitTime: null,
        orderByColumn:'createTime',
        isAsc:'desc',
      },
      // 表单参数
      form: {},
      // 表单校验   
      rules: {
      },
      // 地区列表
      deptlist:[],
      // 企业专员列表
      userlist:[],
    };
  },
  created() {
    this.getList();
    this.getDept();
  },
  watch: {
    'form.areaCode'(res) {
        if(res != '' && res != null && typeof(res) != 'string'){
          let params = {
            'deptId' : res[res.length - 1]
          }
          this.getUserList(params)
        }else{
          this.userlist = []
          this.form.enterpriseCommissioner = null
      }
    }
  },
  methods: {
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listEnterprise(this.queryParams).then(response => {
        this.enterpriseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        corporation: null,
        contactPerson: null,
        contactPhone: null,
        creditCode: null,
        areaCode: null,
        enterpriseSize: null,
        enterpriseCommissioner: null,
        industryType: null,
        enterpriseScale: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        createName: null,
        updateBy: null,
        updateTime: null,
        updateName: null,
        transitTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      getEnterprise(id).then(response => {
        this.form = response.data;

        this.form.enterpriseCommissioner = Number(this.form.enterpriseCommissioner)
        if(this.form.areaCode != null && this.form.areaCode != ''){
          this.form.areaCode = JSON.parse(this.form.areaCode)
          let params = {
            'deptId' : this.form.areaCode[this.form.areaCode.length - 1]
          }
          this.getUserList(params)
        }
        this.open = true;
        this.title = "修改企业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.areaCode = JSON.stringify(this.form.areaCode);
          if (this.form.id != null) {
            updateEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.form = {}
              this.getList();
            });
          } else {
            addEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.form = {}
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业编号为"' + ids + '"的数据项？').then(function() {
        return delEnterprise(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/enterprise/export', {
        ...this.queryParams
      }, `enterprise_${new Date().getTime()}.xlsx`)
    },
    // 获取地区列表
    getDept(){
      let data = {}
      getDeptListByJms(data).then(response => {
        if(response.data.length > 0){
          this.deptlist = response.data.find(chil => chil.deptId == '210').children;
        }
      });
    },
    // 获取用户列表
    getUserList(params){
      listUser(params).then(response => {
        this.userlist = response.rows
        if(this.userlist == null || this.userlist.length == 0){
          this.form.enterpriseCommissioner = null
        }
      })
    }
  }
};
</script>
<style scoped>
::v-deep .el-select{
  width: 100%;
}
::v-deep .el-cascader{
  width: 100%;
}
</style>